package com.xishu.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

//@WebListener(value = "com.xishu.controller")
public class SessionListener implements HttpSessionListener, ServletContextListener {
    private static Logger logger = LoggerFactory.getLogger(SessionListener.class);

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        String sessionId = session.getId();
        System.out.println("sessionId = " + sessionId);
        logger.info("sessionId = " + sessionId);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        String sessionId = session.getId();
        System.out.println("sessionId = " + sessionId);
        logger.info("sessionId = " + sessionId);
    }


    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("servletContextEvent = " + servletContextEvent);
        logger.info("contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("servletContextEvent = " + servletContextEvent);
        logger.info("contextInitialized");
    }
}
